package main

import (
    pb "example.com/http-grpc-server/proto/hello_http" // 引入编译生成的包
    "golang.org/x/net/context"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials/insecure"
    "log"
)

const (
	// Address gRPC服务地址
	Address = "127.0.0.1:50052"
)

/**
* @Description: grpc客户端
* @Author: liangan
* @Date: 2023-09-02_10:42
*/
func main() {
	// 连接
	conn, err := grpc.Dial(Address, grpc.WithTransportCredentials(insecure.NewCredentials()))

	if err != nil {
	   log.Println("连接失败: %v", err)
	}

	defer conn.Close()

	// 初始化客户端
	c := pb.NewHelloHTTPClient(conn)

	// 调用方法
    log.Println("通过gRPC调用SayHello方法")
	reqBody := new(pb.HelloHTTPRequest)
	reqBody.Name = "gRPC"
	resp, err := c.SayHello(context.Background(), reqBody)
	if err != nil {
           log.Fatalf("请求失败: %v", err)
        }
        log.Printf("请求结果: %s", resp.GetMessage())
}
